package com.res.service.impl;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.res.entity.Register;
import com.res.entity.Student;
import com.res.repository.RegisterRepository;
import com.res.service.RegisterService;
@Service
public class RegisterServiceImpl implements RegisterService {

	@Autowired
	private RegisterRepository repository;
	@Override
	public Page<Register> getAllRegister(Pageable pageable , String stuname, String tname, Integer sid, Integer did,String maxTime,String minTime) {
		
		return repository.findAll(get(stuname,tname,sid,did,maxTime,minTime), pageable);
	}
    
public Specification<Register> get(String stuname, String tname, Integer sid, Integer did,String maxTime,String minTime){
		
		return new Specification<Register>() {
			
			@Override
			public Predicate toPredicate(Root<Register> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				Predicate predicate=criteriaBuilder.conjunction();
				List<Expression<Boolean>> expressions = predicate.getExpressions();
				if(!StringUtils.isEmpty(stuname)) {
					expressions.add(criteriaBuilder.like(root.get("stu").get("sname"), "%"+stuname+"%"));
				}				
			
				if(sid!=null&&sid!=0) {
					expressions.add(criteriaBuilder.equal(root.get("stu").get("sub").get("sid"), sid));
				}
				if(did!=null&&did!=0) {
					expressions.add(criteriaBuilder.equal(root.get("teacher").get("department").get("did"), did));
				}
				if(!StringUtils.isEmpty(tname)) {
					expressions.add(criteriaBuilder.like(root.get("teacher").get("tname"), "%"+tname+"%"));
				}
                if(maxTime!=null && minTime!=null) {
					
                	expressions.add(criteriaBuilder.between(root.get("outtime"), minTime, maxTime));
				} 	
				return predicate;
			}
		};
		
	}
	@Override
	public String addR(Register r) {
		try {
			repository.save(r);
			return "添加成功";
		} catch (Exception e) {
			e.printStackTrace();
			return "添加失败";
		}
		
	}

	@Override
	public String update(Register r) {
		try {
			repository.save(r);
			return "修改成功";
		} catch (Exception e) {
			e.printStackTrace();
			return "修改失败";
		}
	}

	@Override
	public String deleteR(Integer rid) {
		try {
			repository.deleteById(rid);
			return "删除成功";
		} catch (Exception e) {
			e.printStackTrace();
			return "删除失败";
		}
	}

	@Override
	public Register getRegisterById(Integer stuid, Integer tid) {
		
		return repository.getRegisterById(stuid, tid);
	}
@Override
	public List<Integer> num() {
		// TODO Auto-generated method stub
		return repository.num();
	}

	@Override
	public List<Register> all() {
		// TODO Auto-generated method stub
		return repository.all();
	}


}